package com.lqq.controller;

import com.lqq.mapper.EmployeeMapper;
import com.lqq.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class EmployeeController {
    @Autowired
    private EmployeeMapper employeeMapper;

    @RequestMapping("/emps")
    public String employ(Model model) {
        List<Employee> employeeList = employeeMapper.selectAllEmployee();
        model.addAttribute("list", employeeList);
        return "emp/list";
    }

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "emp/add";
    }
    @RequestMapping("/add")
    public String addUser(Employee employee) {

        int cow = employeeMapper.addEmployee(employee);
        if (cow > 0) {
            System.out.println("新增员工成功！");
        }
        return "redirect:/emps";
    }

    @RequestMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable("id") int id, Model model) {
        Employee employee = employeeMapper.selectEmployee(id);
        model.addAttribute("emp",employee);
        return "emp/update";
    }
    @RequestMapping("/update")
    public String updateUser(Employee employee) {

        int cow = employeeMapper.updateEmployee(employee);
        if (cow > 0) {
            System.out.println("修改员工成功！");
        }
        return "redirect:/emps";
    }

    @RequestMapping("/delete/{id}")
    public String deleteUser(@PathVariable("id") int id, Model model) {

        int cow = employeeMapper.deleteEmployee(id);
        if (cow > 0) {
            System.out.println("删除员工成功！");
        }
        return "redirect:/emps";
    }
}
